package com.example.spring_seatreservation.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.spring_seatreservation.Bean.MyUser;
import com.example.spring_seatreservation.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class UserExcelListener implements ReadListener<UserExcel> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<MyUser> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private final UserMapper userMapper;

    @Override
    public void invoke(UserExcel userExcel, AnalysisContext analysisContext) {
        if(!StringUtils.hasLength(userExcel.getNumber())){
            throw new RuntimeException("账号不能为空");
        }

        if(!StringUtils.hasLength(userExcel.getUsername())){
            throw new RuntimeException("姓名不能为空");
        }
        MyUser user = new MyUser();
        user.setNumber(userExcel.getNumber());
        user.setBirth(userExcel.getBirth());
        user.setUsername(userExcel.getUsername());
        user.setPhone(userExcel.getPhone());
        user.setEmail(userExcel.getEmail());
        user.setPassword("123456789");
        if (StringUtils.hasLength(userExcel.getSex())) {
            if ("男".equals(userExcel.getSex())) {
                user.setSex(1);
            } else if ("女".equals(userExcel.getSex())) {
                user.setSex(0);
            }
        }
        if (StringUtils.hasLength(userExcel.getType())) {
            if ("学生".equals(userExcel.getSex())) {
                user.setType(0);
            } else if ("老师".equals(userExcel.getSex())) {
                user.setSex(1);
            } else if ("管理员".equals(userExcel.getSex())){
                user.setSex(2);
            }
        }
        LambdaQueryWrapper<MyUser> queryWrapper = Wrappers.<MyUser>lambdaQuery().eq(MyUser::getNumber,userExcel.getNumber());
        if (userMapper.exists(queryWrapper)) {
            throw new RuntimeException("已存在人员 " + userExcel.getNumber());
        }
        cachedDataList.add(user);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            for (MyUser myUser : cachedDataList) {
                userMapper.insert(myUser);
            }
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        for (MyUser myUser : cachedDataList) {
            userMapper.insert(myUser);
        }
    }
}
